package com.ahsjdy.st.stservice.service.impl;

import com.ahsjdy.st.stdao.entity.Navigation;
import com.ahsjdy.st.stdao.entity.Order;
import com.ahsjdy.st.stdao.entity.User;
import com.ahsjdy.st.stdao.mapper.NavigationMapper;
import com.ahsjdy.st.stdao.mapper.OrderMapper;
import com.ahsjdy.st.stdao.mapper.UserMapper;
import com.ahsjdy.stcommon.dto.OrderDTO;
import com.ahsjdy.stcommon.exception.BizException;
import com.ahsjdy.stcommon.util.DateUtils;
import com.ahsjdy.stcommon.vo.SettleRequestVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CustomerService {
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NavigationMapper navigationMapper;

    @Value("${app.merchant.pic.url}")
    protected String fileUrl;

    public void overOrder(String openid, int orderId) {
        User user = userMapper.getByOpenId(openid);
        if (null == user) {
            throw new BizException("无权操作");
        }

        Order order = orderMapper.selectByPrimaryKey(orderId);
        if (null == order) {
            throw new BizException("非法请求");
        }

        if (order.getStatus() != 1) {
            return;
        }

        int rows = orderMapper.updateStatus(order.getId(), order.getStatus(), 2);

        if (rows != 1) {
            throw new BizException("操作失败");
        }
    }

    public List<OrderDTO> queryOrders(String openid) {

        Integer status = null;

        boolean isAdmin = false;

        //如果是管理员 则查询全部且状态为1的待回收的订单
        User user = userMapper.getByOpenId(openid);
        if (null != user) {
            status = 1;
            openid = null;
            isAdmin = true;
        }

        List<Order> orders = orderMapper.listOrder(openid, status);
        List<OrderDTO> dtos = new ArrayList(orders.size());

        for (Order order : orders) {
            OrderDTO orderDTO = new OrderDTO();
            dtos.add(orderDTO);

            orderDTO.setAdmin(isAdmin);

            orderDTO.setAddress(order.getAddress());
            orderDTO.setCreateTime(DateUtils.dateToString(order.getCreateTime()));
            orderDTO.setId(order.getId());

            Navigation navFirst = navigationMapper.selectByPrimaryKey(order.getNavfirst());
            Navigation navSecond = navigationMapper.selectByPrimaryKey(order.getNavsecond());

            orderDTO.setNavfirst(navFirst.getNavname());
            orderDTO.setNavsecond(navSecond.getNavname());
            orderDTO.setPhone(order.getPhone());
            String picUrls[] = order.getPicturenames().split(",");
            for (int i=0; i<picUrls.length; i++) {
                if (i==0) {
                    orderDTO.setPicUrl1(fileUrl + picUrls[i]);
                }
                if (i==1) {
                    orderDTO.setPicUrl2(fileUrl + picUrls[i]);
                }
                if (i==2) {
                    orderDTO.setPicUrl3(fileUrl + picUrls[i]);
                }
            }

            orderDTO.setRemark(order.getRemark());
            orderDTO.setStatus(order.getStatus());
            orderDTO.setStatusValue(order.getStatus() == 1 ? "待回收" : "已回收");

        }

        return dtos;
    }

    public void settle(SettleRequestVO vo) {

        if (StringUtils.isBlank(vo.getPhone())) {
            throw new BizException("请输入手机号码");
        }

        if (StringUtils.isBlank(vo.getAddress()) || vo.getAddress().length() < 3) {
            throw new BizException("请输入详细地址");
        }

        if (StringUtils.isBlank(vo.getOpenid())) {
            throw new BizException("非法请求");
        }

        StringBuilder sx = new StringBuilder();
        sx.append(vo.getPicName1());
        sx.append(",");
        sx.append(vo.getPicName2());
        sx.append(",");
        sx.append(vo.getPicName3());

        Order order = new Order();
        order.setAddress(vo.getAddress());
        order.setCreateTime(new Date());
        order.setModifyTime(order.getCreateTime());
        order.setNavfirst(vo.getNavFirst());
        order.setOpenid(vo.getOpenid());
        order.setNavsecond(vo.getNavSecond());
        order.setPhone(vo.getPhone());
        order.setPicturenames(sx.toString());
        order.setRemark(vo.getRemark());
        order.setStatus(1);
        orderMapper.insert(order);
    }
}
